home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libiberty / cplus-dem.c < prev    next >
C/C++ Source or Header  |  1994-08-19  |  68KB  |  2,927 lines

  1. /* Demangler for GNU C++ 
  2.    Copyright 1989, 1991 Free Software Foundation, Inc.
  3.    Written by James Clark (jjc@jclark.uucp)
  4.    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  5.    
  6. This file is part of the libiberty library.
  7. Libiberty is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11.  
  12. Libiberty is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with libiberty; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
  23.  
  24.    This file imports xmalloc and xrealloc, which are like malloc and
  25.    realloc except that they generate a fatal error if there is no
  26.    available memory. */
  27.  
  28. #include <demangle.h>
  29. #undef CURRENT_DEMANGLING_STYLE
  30. #define CURRENT_DEMANGLING_STYLE work->options
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34.  
  35. #include "libiberty.h"
  36.  
  37. extern char *strstr ();
  38.  
  39. /* In order to allow a single demangler executable to demangle strings
  40.    using various common values of CPLUS_MARKER, as well as any specific
  41.    one set at compile time, we maintain a string containing all the
  42.    commonly used ones, and check to see if the marker we are looking for
  43.    is in that string.  CPLUS_MARKER is usually '$' on systems where the
  44.    assembler can deal with that.  Where the assembler can't, it's usually
  45.    '.' (but on many systems '.' is used for other things).  We put the
  46.    current defined CPLUS_MARKER first (which defaults to '$'), followed
  47.    by the next most common value, followed by an explicit '$' in case
  48.    the value of CPLUS_MARKER is not '$'.
  49.  
  50.    We could avoid this if we could just get g++ to tell us what the actual
  51.    cplus marker character is as part of the debug information, perhaps by
  52.    ensuring that it is the character that terminates the gcc<n>_compiled
  53.    marker symbol (FIXME). */
  54.  
  55. #if !defined (CPLUS_MARKER)
  56. #define CPLUS_MARKER '$'
  57. #endif
  58.  
  59. enum demangling_styles current_demangling_style = gnu_demangling;
  60.  
  61. static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
  62.  
  63. void
  64. set_cplus_marker_for_demangling (ch)
  65.      int ch;
  66. {
  67.     cplus_markers[0] = ch;
  68. }
  69.  
  70. /* Stuff that is shared between sub-routines.
  71.  * Using a shared structure allows cplus_demangle to be reentrant. */
  72.  
  73. struct work_stuff
  74. {
  75.   int options;
  76.   char **typevec;
  77.   int ntypes;
  78.   int typevec_size;
  79.   int constructor;
  80.   int destructor;
  81.   int static_type;    /* A static member function */
  82.   int const_type;    /* A const member function */
  83. };
  84.  
  85. #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
  86. #define PRINT_ARG_TYPES       (work -> options & DMGL_PARAMS)
  87.  
  88. static CONST struct optable
  89. {
  90.   CONST char *in;
  91.   CONST char *out;
  92.   int flags;
  93. } optable[] = {
  94.   {"nw",      " new",    DMGL_ANSI},    /* new (1.92,     ansi) */
  95.   {"dl",      " delete",    DMGL_ANSI},    /* new (1.92,     ansi) */
  96.   {"new",      " new",    0},        /* old (1.91,     and 1.x) */
  97.   {"delete",      " delete",    0},        /* old (1.91,     and 1.x) */
  98.   {"vn",      " new []",    DMGL_ANSI},    /* GNU, pending ansi */
  99.   {"vd",      " delete []",    DMGL_ANSI},    /* GNU, pending ansi */
  100.   {"as",      "=",        DMGL_ANSI},    /* ansi */
  101.   {"ne",      "!=",        DMGL_ANSI},    /* old, ansi */
  102.   {"eq",      "==",        DMGL_ANSI},    /* old,    ansi */
  103.   {"ge",      ">=",        DMGL_ANSI},    /* old,    ansi */
  104.   {"gt",      ">",        DMGL_ANSI},    /* old,    ansi */
  105.   {"le",      "<=",        DMGL_ANSI},    /* old,    ansi */
  106.   {"lt",      "<",        DMGL_ANSI},    /* old,    ansi */
  107.   {"plus",      "+",        0},        /* old */
  108.   {"pl",      "+",        DMGL_ANSI},    /* ansi */
  109.   {"apl",      "+=",        DMGL_ANSI},    /* ansi */
  110.   {"minus",      "-",        0},        /* old */
  111.   {"mi",      "-",        DMGL_ANSI},    /* ansi */
  112.   {"ami",      "-=",        DMGL_ANSI},    /* ansi */
  113.   {"mult",      "*",        0},        /* old */
  114.   {"ml",      "*",        DMGL_ANSI},    /* ansi */
  115.   {"amu",      "*=",        DMGL_ANSI},    /* ansi (ARM/Lucid) */
  116.   {"aml",      "*=",        DMGL_ANSI},    /* ansi (GNU/g++) */
  117.   {"convert",      "+",        0},        /* old (unary +) */
  118.   {"negate",      "-",        0},        /* old (unary -) */
  119.   {"trunc_mod",      "%",        0},        /* old */
  120.   {"md",      "%",        DMGL_ANSI},    /* ansi */
  121.   {"amd",      "%=",        DMGL_ANSI},    /* ansi */
  122.   {"trunc_div",      "/",        0},        /* old */
  123.   {"dv",      "/",        DMGL_ANSI},    /* ansi */
  124.   {"adv",      "/=",        DMGL_ANSI},    /* ansi */
  125.   {"truth_andif", "&&",        0},        /* old */
  126.   {"aa",      "&&",        DMGL_ANSI},    /* ansi */
  127.   {"truth_orif",  "||",        0},        /* old */
  128.   {"oo",      "||",        DMGL_ANSI},    /* ansi */
  129.   {"truth_not",      "!",        0},        /* old */
  130.   {"nt",      "!",        DMGL_ANSI},    /* ansi */
  131.   {"postincrement","++",    0},        /* old */
  132.   {"pp",      "++",        DMGL_ANSI},    /* ansi */
  133.   {"postdecrement","--",    0},        /* old */
  134.   {"mm",      "--",        DMGL_ANSI},    /* ansi */
  135.   {"bit_ior",      "|",        0},        /* old */
  136.   {"or",      "|",        DMGL_ANSI},    /* ansi */
  137.   {"aor",      "|=",        DMGL_ANSI},    /* ansi */
  138.   {"bit_xor",      "^",        0},        /* old */
  139.   {"er",      "^",        DMGL_ANSI},    /* ansi */
  140.   {"aer",      "^=",        DMGL_ANSI},    /* ansi */
  141.   {"bit_and",      "&",        0},        /* old */
  142.   {"ad",      "&",        DMGL_ANSI},    /* ansi */
  143.   {"aad",      "&=",        DMGL_ANSI},    /* ansi */
  144.   {"bit_not",      "~",        0},        /* old */
  145.   {"co",      "~",        DMGL_ANSI},    /* ansi */
  146.   {"call",      "()",        0},        /* old */
  147.   {"cl",      "()",        DMGL_ANSI},    /* ansi */
  148.   {"alshift",      "<<",        0},        /* old */
  149.   {"ls",      "<<",        DMGL_ANSI},    /* ansi */
  150.   {"als",      "<<=",    DMGL_ANSI},    /* ansi */
  151.   {"arshift",      ">>",        0},        /* old */
  152.   {"rs",      ">>",        DMGL_ANSI},    /* ansi */
  153.   {"ars",      ">>=",    DMGL_ANSI},    /* ansi */
  154.   {"component",      "->",        0},        /* old */
  155.   {"pt",      "->",        DMGL_ANSI},    /* ansi; Lucid C++ form */
  156.   {"rf",      "->",        DMGL_ANSI},    /* ansi; ARM/GNU form */
  157.   {"indirect",      "*",        0},        /* old */
  158.   {"method_call",  "->()",    0},        /* old */
  159.   {"addr",      "&",        0},        /* old (unary &) */
  160.   {"array",      "[]",        0},        /* old */
  161.   {"vc",      "[]",        DMGL_ANSI},    /* ansi */
  162.   {"compound",      ", ",        0},        /* old */
  163.   {"cm",      ", ",        DMGL_ANSI},    /* ansi */
  164.   {"cond",      "?:",        0},        /* old */
  165.   {"cn",      "?:",        DMGL_ANSI},    /* psuedo-ansi */
  166.   {"max",      ">?",        0},        /* old */
  167.   {"mx",      ">?",        DMGL_ANSI},    /* psuedo-ansi */
  168.   {"min",      "<?",        0},        /* old */
  169.   {"mn",      "<?",        DMGL_ANSI},    /* psuedo-ansi */
  170.   {"nop",      "",        0},        /* old (for operator=) */
  171.   {"rm",      "->*",    DMGL_ANSI}    /* ansi */
  172. };
  173.  
  174.  
  175. typedef struct string        /* Beware: these aren't required to be */
  176. {                /*  '\0' terminated. */
  177.   char *b;            /* pointer to start of string */
  178.   char *p;            /* pointer after last character */
  179.   char *e;            /* pointer after end of allocated space */
  180. } string;
  181.  
  182. #define STRING_EMPTY(str)    ((str) -> b == (str) -> p)
  183. #define PREPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  184.                    string_prepend(str, " ");}
  185. #define APPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  186.                    string_append(str, " ");}
  187.  
  188. #define ARM_VTABLE_STRING "__vtbl__"    /* Lucid/ARM virtual table prefix */
  189. #define ARM_VTABLE_STRLEN 8        /* strlen (ARM_VTABLE_STRING) */
  190.  
  191. /* Prototypes for local functions */
  192.  
  193. static char *
  194. mop_up PARAMS ((struct work_stuff *, string *, int));
  195.  
  196. #if 0
  197. static int
  198. demangle_method_args PARAMS ((struct work_stuff *work, CONST char **, string *));
  199. #endif
  200.  
  201. static int
  202. demangle_template PARAMS ((struct work_stuff *work, CONST char **, string *,
  203.                string *));
  204.  
  205. static int
  206. demangle_qualified PARAMS ((struct work_stuff *, CONST char **, string *,
  207.                 int, int));
  208.  
  209. static int
  210. demangle_class PARAMS ((struct work_stuff *, CONST char **, string *));
  211.  
  212. static int
  213. demangle_fund_type PARAMS ((struct work_stuff *, CONST char **, string *));
  214.  
  215. static int
  216. demangle_signature PARAMS ((struct work_stuff *, CONST char **, string *));
  217.  
  218. static int
  219. demangle_prefix PARAMS ((struct work_stuff *, CONST char **, string *));
  220.  
  221. static int
  222. gnu_special PARAMS ((struct work_stuff *, CONST char **, string *));
  223.  
  224. static int
  225. arm_special PARAMS ((struct work_stuff *, CONST char **, string *));
  226.  
  227. static void
  228. string_need PARAMS ((string *, int));
  229.  
  230. static void
  231. string_delete PARAMS ((string *));
  232.  
  233. static void
  234. string_init PARAMS ((string *));
  235.  
  236. static void
  237. string_clear PARAMS ((string *));
  238.  
  239. #if 0
  240. static int
  241. string_empty PARAMS ((string *));
  242. #endif
  243.  
  244. static void
  245. string_append PARAMS ((string *, CONST char *));
  246.  
  247. static void
  248. string_appends PARAMS ((string *, string *));
  249.  
  250. static void
  251. string_appendn PARAMS ((string *, CONST char *, int));
  252.  
  253. static void
  254. string_prepend PARAMS ((string *, CONST char *));
  255.  
  256. static void
  257. string_prependn PARAMS ((string *, CONST char *, int));
  258.  
  259. static int
  260. get_count PARAMS ((CONST char **, int *));
  261.  
  262. static int
  263. consume_count PARAMS ((CONST char **));
  264.  
  265. static int
  266. demangle_args PARAMS ((struct work_stuff *, CONST char **, string *));
  267.  
  268. static int
  269. do_type PARAMS ((struct work_stuff *, CONST char **, string *));
  270.  
  271. static int
  272. do_arg PARAMS ((struct work_stuff *, CONST char **, string *));
  273.  
  274. static void
  275. demangle_function_name PARAMS ((struct work_stuff *, CONST char **, string *,
  276.                 CONST char *));
  277.  
  278. static void
  279. remember_type PARAMS ((struct work_stuff *, CONST char *, int));
  280.  
  281. static void
  282. forget_types PARAMS ((struct work_stuff *));
  283.  
  284. static void
  285. string_prepends PARAMS ((string *, string *));
  286.  
  287. /*  Translate count to integer, consuming tokens in the process.
  288.     Conversion terminates on the first non-digit character.
  289.     Trying to consume something that isn't a count results in
  290.     no consumption of input and a return of 0. */
  291.  
  292. static int
  293. consume_count (type)
  294.     CONST char **type;
  295. {
  296.     int count = 0;
  297.  
  298.     while (isdigit (**type))
  299.       {
  300.     count *= 10;
  301.     count += **type - '0';
  302.     (*type)++;
  303.       }
  304.     return (count);
  305. }
  306.  
  307. int
  308. cplus_demangle_opname (opname, result, options)
  309.      char *opname;
  310.      char *result;
  311.      int options;
  312. {
  313.   int len, i, len1, ret;
  314.   string type;
  315.   struct work_stuff work[1];
  316.   CONST char *tem;
  317.  
  318.   len = strlen(opname);
  319.   result[0] = '\0';
  320.   ret = 0;
  321.   work->options = options;
  322.   
  323.   if (opname[0] == '_' && opname[1] == '_'
  324.       && opname[2] == 'o' && opname[3] == 'p')
  325.     {
  326.       /* ANSI.  */
  327.       /* type conversion operator.  */
  328.       tem = opname + 4;
  329.       if (do_type (work, &tem, &type))
  330.     {
  331.       strcat (result, "operator ");
  332.       strncat (result, type.b, type.p - type.b);
  333.       string_delete (&type);
  334.       ret = 1;
  335.     }
  336.     }
  337.   else if (opname[0] == '_' && opname[1] == '_'
  338.        && opname[2] >= 'a' && opname[2] <= 'z'
  339.        && opname[3] >= 'a' && opname[3] <= 'z')
  340.     {
  341.       if (opname[4] == '\0')
  342.     {
  343.       /* Operator.  */
  344.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  345.         {
  346.           if (strlen (optable[i].in) == 2
  347.           && memcmp (optable[i].in, opname + 2, 2) == 0)
  348.         {
  349.           strcat (result, "operator");
  350.           strcat (result, optable[i].out);
  351.           ret = 1;
  352.           break;
  353.         }
  354.         }
  355.     }
  356.       else
  357.     {
  358.       if (opname[2] == 'a' && opname[5] == '\0')
  359.         {
  360.           /* Assignment. */
  361.           for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  362.         {
  363.           if (strlen (optable[i].in) == 3
  364.               && memcmp (optable[i].in, opname + 2, 3) == 0)
  365.             {
  366.               strcat (result, "operator");
  367.               strcat (result, optable[i].out);
  368.               ret = 1;
  369.               break;
  370.             }              
  371.         }
  372.         }
  373.     }
  374.     }
  375.   else if (len >= 3 
  376.       && opname[0] == 'o'
  377.       && opname[1] == 'p'
  378.       && strchr (cplus_markers, opname[2]) != NULL)
  379.     {
  380.       /* see if it's an assignment expression */
  381.       if (len >= 10 /* op$assign_ */
  382.       && memcmp (opname + 3, "assign_", 7) == 0)
  383.     {
  384.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  385.         {
  386.           len1 = len - 10;
  387.           if (strlen (optable[i].in) == len1
  388.           && memcmp (optable[i].in, opname + 10, len1) == 0)
  389.         {
  390.           strcat (result, "operator");
  391.           strcat (result, optable[i].out);
  392.           strcat (result, "=");
  393.           ret = 1;
  394.           break;
  395.         }
  396.         }
  397.     }
  398.       else
  399.     {
  400.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  401.         {
  402.           len1 = len - 3;
  403.           if (strlen (optable[i].in) == len1 
  404.           && memcmp (optable[i].in, opname + 3, len1) == 0)
  405.         {
  406.           strcat (result, "operator");
  407.           strcat (result, optable[i].out);
  408.           ret = 1;
  409.           break;
  410.         }
  411.         }
  412.     }
  413.     }
  414.   else if (len >= 5 && memcmp (opname, "type", 4) == 0
  415.        && strchr (cplus_markers, opname[4]) != NULL)
  416.     {
  417.       /* type conversion operator */
  418.       tem = opname + 5;
  419.       if (do_type (work, &tem, &type))
  420.     {
  421.       strcat (result, "operator ");
  422.       strncat (result, type.b, type.p - type.b);
  423.       string_delete (&type);
  424.       ret = 1;
  425.     }
  426.     }
  427.   return ret;
  428.  
  429. }
  430. /* Takes operator name as e.g. "++" and returns mangled
  431.    operator name (e.g. "postincrement_expr"), or NULL if not found.
  432.  
  433.    If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
  434.    if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
  435.  
  436. char *
  437. cplus_mangle_opname (opname, options)
  438.      char *opname;
  439.      int options;
  440. {
  441.   int i;
  442.   int len;
  443.  
  444.   len = strlen (opname);
  445.   for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  446.     {
  447.       if (strlen (optable[i].out) == len
  448.       && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
  449.       && memcmp (optable[i].out, opname, len) == 0)
  450.     return ((char *)optable[i].in);
  451.     }
  452.   return (0);
  453. }
  454.  
  455. /* check to see whether MANGLED can match TEXT in the first TEXT_LEN
  456.    characters. */
  457.  
  458. int cplus_match (mangled, text, text_len)
  459.      CONST char *mangled;
  460.      char *text;
  461.      int text_len;
  462. {
  463.   if (strncmp (mangled, text, text_len) != 0) {
  464.     return(0); /* cannot match either */
  465.   } else {
  466.     return(1); /* matches mangled, may match demangled */
  467.   }
  468. }
  469.  
  470. /* char *cplus_demangle (const char *mangled, int options)
  471.  
  472.    If MANGLED is a mangled function name produced by GNU C++, then
  473.    a pointer to a malloced string giving a C++ representation
  474.    of the name will be returned; otherwise NULL will be returned.
  475.    It is the caller's responsibility to free the string which
  476.    is returned.
  477.  
  478.    The OPTIONS arg may contain one or more of the following bits:
  479.  
  480.        DMGL_ANSI    ANSI qualifiers such as `const' and `void' are
  481.             included.
  482.     DMGL_PARAMS    Function parameters are included.
  483.  
  484.    For example,
  485.    
  486.    cplus_demangle ("foo__1Ai", DMGL_PARAMS)        => "A::foo(int)"
  487.    cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI)    => "A::foo(int)"
  488.    cplus_demangle ("foo__1Ai", 0)            => "A::foo"
  489.  
  490.    cplus_demangle ("foo__1Afe", DMGL_PARAMS)        => "A::foo(float,...)"
  491.    cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
  492.    cplus_demangle ("foo__1Afe", 0)            => "A::foo"
  493.  
  494.    Note that any leading underscores, or other such characters prepended by
  495.    the compilation system, are presumed to have already been stripped from
  496.    MANGLED.  */
  497.  
  498. char *
  499. cplus_demangle (mangled, options)
  500.      CONST char *mangled;
  501.      int options;
  502. {
  503.   string decl;
  504.   int success = 0;
  505.   struct work_stuff work[1];
  506.   char *demangled = NULL;
  507.  
  508.   if ((mangled != NULL) && (*mangled != '\0'))
  509.     {
  510.       memset ((char *) work, 0, sizeof (work));
  511.       work -> options = options;
  512.       if ((work->options & DMGL_STYLE_MASK) == 0)
  513.     work->options |= (int)current_demangling_style & DMGL_STYLE_MASK;
  514.       
  515.       string_init (&decl);
  516.  
  517.       /* First check to see if gnu style demangling is active and if the
  518.      string to be demangled contains a CPLUS_MARKER.  If so, attempt to
  519.      recognize one of the gnu special forms rather than looking for a
  520.      standard prefix.  In particular, don't worry about whether there
  521.      is a "__" string in the mangled string.  Consider "_$_5__foo" for
  522.      example. */
  523.  
  524.       if ((AUTO_DEMANGLING || GNU_DEMANGLING))
  525.     {
  526.       success = gnu_special (work, &mangled, &decl);
  527.     }
  528.       if (!success)
  529.     {
  530.       success = demangle_prefix (work, &mangled, &decl);
  531.     }
  532.       if (success && (*mangled != '\0'))
  533.     {
  534.       success = demangle_signature (work, &mangled, &decl);
  535.     }
  536.       if (work->constructor == 2)
  537.         {
  538.           string_prepend(&decl, "global constructors keyed to ");
  539.           work->constructor = 0;
  540.         }
  541.       else if (work->destructor == 2)
  542.         {
  543.           string_prepend(&decl, "global destructors keyed to ");
  544.           work->destructor = 0;
  545.         }
  546.       demangled = mop_up (work, &decl, success);
  547.     }
  548.   return (demangled);
  549. }
  550.  
  551. static char *
  552. mop_up (work, declp, success)
  553.      struct work_stuff *work;
  554.      string *declp;
  555.      int success;
  556. {
  557.   char *demangled = NULL;
  558.  
  559.   /* Discard the remembered types, if any. */
  560.   
  561.   forget_types (work);
  562.   if (work -> typevec != NULL)
  563.     {
  564.       free ((char *) work -> typevec);
  565.     }
  566.   
  567.   /* If demangling was successful, ensure that the demangled string is null
  568.      terminated and return it.  Otherwise, free the demangling decl. */
  569.   
  570.   if (!success)
  571.     {
  572.       string_delete (declp);
  573.     }
  574.   else
  575.     {
  576.       string_appendn (declp, "", 1);
  577.       demangled = declp -> b;
  578.     }
  579.   return (demangled);
  580. }
  581.  
  582. /*
  583.  
  584. LOCAL FUNCTION
  585.  
  586.     demangle_signature -- demangle the signature part of a mangled name
  587.  
  588. SYNOPSIS
  589.  
  590.     static int
  591.     demangle_signature (struct work_stuff *work, const char **mangled,
  592.                 string *declp);
  593.  
  594. DESCRIPTION
  595.  
  596.     Consume and demangle the signature portion of the mangled name.
  597.  
  598.     DECLP is the string where demangled output is being built.  At
  599.     entry it contains the demangled root name from the mangled name
  600.     prefix.  I.E. either a demangled operator name or the root function
  601.     name.  In some special cases, it may contain nothing.
  602.  
  603.     *MANGLED points to the current unconsumed location in the mangled
  604.     name.  As tokens are consumed and demangling is performed, the
  605.     pointer is updated to continuously point at the next token to
  606.     be consumed.
  607.  
  608.     Demangling GNU style mangled names is nasty because there is no
  609.     explicit token that marks the start of the outermost function
  610.     argument list.
  611. */
  612.  
  613. static int
  614. demangle_signature (work, mangled, declp)
  615.      struct work_stuff *work;
  616.      CONST char **mangled;
  617.      string *declp;
  618. {
  619.   int success = 1;
  620.   int func_done = 0;
  621.   int expect_func = 0;
  622.   CONST char *oldmangled = NULL;
  623.   string trawname;
  624.   string tname;
  625.  
  626.   while (success && (**mangled != '\0'))
  627.     {
  628.       switch (**mangled)
  629.     {
  630.       case 'Q':
  631.         oldmangled = *mangled;
  632.         success = demangle_qualified (work, mangled, declp, 1, 0);
  633.         if (success)
  634.           {
  635.         remember_type (work, oldmangled, *mangled - oldmangled);
  636.           }
  637.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  638.           {
  639.         expect_func = 1;
  640.           }
  641.         oldmangled = NULL;
  642.         break;
  643.       
  644.       case 'S':
  645.         /* Static member function */
  646.         if (oldmangled == NULL)
  647.           {
  648.         oldmangled = *mangled;
  649.           }
  650.         (*mangled)++;
  651.         work -> static_type = 1;
  652.         break;
  653.  
  654.       case 'C':
  655.         /* a const member function */
  656.         if (oldmangled == NULL)
  657.           {
  658.         oldmangled = *mangled;
  659.           }
  660.         (*mangled)++;
  661.         work -> const_type = 1;
  662.         break;
  663.       
  664.       case '0': case '1': case '2': case '3': case '4':
  665.       case '5': case '6': case '7': case '8': case '9':
  666.         if (oldmangled == NULL)
  667.           {
  668.         oldmangled = *mangled;
  669.           }
  670.         success = demangle_class (work, mangled, declp);
  671.         if (success)
  672.           {
  673.         remember_type (work, oldmangled, *mangled - oldmangled);
  674.           }
  675.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  676.           {
  677.         expect_func = 1;
  678.           }
  679.         oldmangled = NULL;
  680.         break;
  681.       
  682.       case 'F':
  683.         /* Function */
  684.         /* ARM style demangling includes a specific 'F' character after
  685.          the class name.  For GNU style, it is just implied.  So we can
  686.          safely just consume any 'F' at this point and be compatible
  687.          with either style. */
  688.  
  689.         oldmangled = NULL;
  690.         func_done = 1;
  691.         (*mangled)++;
  692.  
  693.         /* For lucid/ARM style we have to forget any types we might
  694.            have remembered up to this point, since they were not argument
  695.            types.  GNU style considers all types seen as available for
  696.            back references.  See comment in demangle_args() */
  697.  
  698.         if (LUCID_DEMANGLING || ARM_DEMANGLING)
  699.           {
  700.         forget_types (work);
  701.           }
  702.         success = demangle_args (work, mangled, declp);
  703.         break;
  704.       
  705.       case 't':
  706.         /* G++ Template */
  707.         string_init(&trawname); 
  708.         string_init(&tname);
  709.             if (oldmangled == NULL)
  710.               {
  711.                 oldmangled = *mangled;
  712.               }
  713.         success = demangle_template (work, mangled, &tname, &trawname);
  714.             if (success)
  715.               {
  716.                 remember_type (work, oldmangled, *mangled - oldmangled);
  717.               }
  718.         string_append(&tname, "::");
  719.         string_prepends(declp, &tname);
  720.           if (work -> destructor & 1)
  721.               {
  722.               string_prepend (&trawname, "~");
  723.               string_appends (declp, &trawname);
  724.         work->destructor -= 1;
  725.               }
  726.           if ((work->constructor & 1) || (work->destructor & 1))
  727.               {
  728.               string_appends (declp, &trawname);
  729.         work->constructor -= 1;
  730.               }
  731.         string_delete(&trawname);
  732.         string_delete(&tname);
  733.         oldmangled = NULL;
  734.         expect_func = 1;
  735.         break;
  736.  
  737.       case '_':
  738.         /* At the outermost level, we cannot have a return type specified,
  739.            so if we run into another '_' at this point we are dealing with
  740.            a mangled name that is either bogus, or has been mangled by
  741.            some algorithm we don't know how to deal with.  So just
  742.            reject the entire demangling. */
  743.         success = 0;
  744.         break;
  745.  
  746.       default:
  747.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  748.           {
  749.         /* Assume we have stumbled onto the first outermost function
  750.            argument token, and start processing args. */
  751.         func_done = 1;
  752.         success = demangle_args (work, mangled, declp);
  753.           }
  754.         else
  755.           {
  756.         /* Non-GNU demanglers use a specific token to mark the start
  757.            of the outermost function argument tokens.  Typically 'F',
  758.            for ARM-demangling, for example.  So if we find something
  759.            we are not prepared for, it must be an error. */
  760.         success = 0;
  761.           }
  762.         break;
  763.     }
  764. /*
  765.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  766. */
  767.     {
  768.       if (success && expect_func)
  769.         {
  770.           func_done = 1;
  771.           success = demangle_args (work, mangled, declp);
  772.         }
  773.     }
  774.     }
  775.   if (success && !func_done)
  776.     {
  777.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  778.     {
  779.       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
  780.          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
  781.          first case, and need to ensure that the '(void)' gets added to
  782.          the current declp.  Note that with ARM, the first case
  783.          represents the name of a static data member 'foo::bar',
  784.          which is in the current declp, so we leave it alone. */
  785.       success = demangle_args (work, mangled, declp);
  786.     }
  787.     }
  788.   if (success && work -> static_type && PRINT_ARG_TYPES)
  789.     {
  790.       string_append (declp, " static");
  791.     }
  792.   if (success && work -> const_type && PRINT_ARG_TYPES)
  793.     {
  794.       string_append (declp, " const");
  795.     }
  796.   return (success);
  797. }
  798.  
  799. #if 0
  800.  
  801. static int
  802. demangle_method_args (work, mangled, declp)
  803.      struct work_stuff *work;
  804.      CONST char **mangled;
  805.      string *declp;
  806. {
  807.   int success = 0;
  808.  
  809.   if (work -> static_type)
  810.     {
  811.       string_append (declp, *mangled + 1);
  812.       *mangled += strlen (*mangled);
  813.       success = 1;
  814.     }
  815.   else
  816.     {
  817.       success = demangle_args (work, mangled, declp);
  818.     }
  819.   return (success);
  820. }
  821.  
  822. #endif
  823.  
  824. static int
  825. demangle_template (work, mangled, tname, trawname)
  826.      struct work_stuff *work;
  827.      CONST char **mangled;
  828.      string *tname;
  829.      string *trawname;
  830. {
  831.   int i;
  832.   int is_pointer;
  833.   int is_real;
  834.   int is_integral;
  835.   int is_char;
  836.   int r;
  837.   int need_comma = 0;
  838.   int success = 0;
  839.   int done;
  840.   CONST char *old_p;
  841.   CONST char *start;
  842.   int symbol_len;
  843.   string temp;
  844.  
  845.   (*mangled)++;
  846.   start = *mangled;
  847.   /* get template name */
  848.   if ((r = consume_count (mangled)) == 0 || strlen (*mangled) < r)
  849.     {
  850.       return (0);
  851.     }
  852.   if (trawname)
  853.     string_appendn (trawname, *mangled, r);
  854.   string_appendn (tname, *mangled, r);
  855.   *mangled += r;
  856.   string_append (tname, "<");
  857.   /* get size of template parameter list */
  858.   if (!get_count (mangled, &r))
  859.     {
  860.       return (0);
  861.     }
  862.   for (i = 0; i < r; i++)
  863.     {
  864.       if (need_comma)
  865.     {
  866.       string_append (tname, ", ");
  867.     }
  868.       /* Z for type parameters */
  869.       if (**mangled == 'Z')
  870.     {
  871.       (*mangled)++;
  872.       /* temp is initialized in do_type */
  873.       success = do_type (work, mangled, &temp);
  874.       if (success)
  875.         {
  876.           string_appends (tname, &temp);
  877.         }
  878.       string_delete(&temp);
  879.       if (!success)
  880.         {
  881.           break;
  882.         }
  883.     }
  884.       else
  885.     {
  886.       /* otherwise, value parameter */
  887.       old_p  = *mangled;
  888.       is_pointer = 0;
  889.       is_real = 0;
  890.       is_integral = 0;
  891.           is_char = 0;
  892.       done = 0;
  893.       /* temp is initialized in do_type */
  894.       success = do_type (work, mangled, &temp);
  895. /*
  896.       if (success)
  897.         {
  898.           string_appends (tname, &temp);
  899.         }
  900. */
  901.       string_delete(&temp);
  902.       if (!success)
  903.         {
  904.           break;
  905.         }
  906. /*
  907.       string_append (tname, "=");
  908. */
  909.       while (*old_p && !done)
  910.         {    
  911.           switch (*old_p)
  912.         {
  913.           case 'P':
  914.           case 'p':
  915.           case 'R':
  916.             done = is_pointer = 1;
  917.             break;
  918.           case 'C':    /* const */
  919.           case 'S':    /* explicitly signed [char] */
  920.           case 'U':    /* unsigned */
  921.           case 'V':    /* volatile */
  922.           case 'F':    /* function */
  923.           case 'M':    /* member function */
  924.           case 'O':    /* ??? */
  925.             old_p++;
  926.             continue;
  927.           case 'Q':    /* qualified name */
  928.                     done = is_integral = 1;
  929.                     break;
  930.           case 'T':    /* remembered type */
  931.             abort ();
  932.             break;
  933.           case 'v':    /* void */
  934.             abort ();
  935.             break;
  936.           case 'x':    /* long long */
  937.           case 'l':    /* long */
  938.           case 'i':    /* int */
  939.           case 's':    /* short */
  940.           case 'w':    /* wchar_t */
  941.             done = is_integral = 1;
  942.             break;
  943.           case 'c':    /* char */
  944.             done = is_char = 1;
  945.             break;
  946.           case 'r':    /* long double */
  947.           case 'd':    /* double */
  948.           case 'f':    /* float */
  949.             done = is_real = 1;
  950.             break;
  951.           default:
  952.             /* it's probably user defined type, let's assume
  953.                it's integeral, it seems hard to figure out
  954.                what it really is */
  955.             done = is_integral = 1;
  956.         }
  957.         }
  958.       if (is_integral)
  959.         {
  960.           if (**mangled == 'm')
  961.         {
  962.           string_appendn (tname, "-", 1);
  963.           (*mangled)++;
  964.         }
  965.           while (isdigit (**mangled))    
  966.         {
  967.           string_appendn (tname, *mangled, 1);
  968.           (*mangled)++;
  969.         }
  970.         }
  971.       else if (is_char)
  972.         {
  973.             char tmp[2];
  974.             int val;
  975.               if (**mangled == 'm')
  976.                 {
  977.                   string_appendn (tname, "-", 1);
  978.                   (*mangled)++;
  979.                 }
  980.           string_appendn (tname, "'", 1);
  981.               val = consume_count(mangled);
  982.           if (val == 0)
  983.         {
  984.           success = 0;
  985.           break;
  986.                 }
  987.               tmp[0] = (char)val;
  988.               tmp[1] = '\0';
  989.               string_appendn (tname, &tmp[0], 1);
  990.           string_appendn (tname, "'", 1);
  991.         }
  992.       else if (is_real)
  993.         {
  994.           if (**mangled == 'm')
  995.         {
  996.           string_appendn (tname, "-", 1);
  997.           (*mangled)++;
  998.         }
  999.           while (isdigit (**mangled))    
  1000.         {
  1001.           string_appendn (tname, *mangled, 1);
  1002.           (*mangled)++;
  1003.         }
  1004.           if (**mangled == '.') /* fraction */
  1005.         {
  1006.           string_appendn (tname, ".", 1);
  1007.           (*mangled)++;
  1008.           while (isdigit (**mangled))    
  1009.             {
  1010.               string_appendn (tname, *mangled, 1);
  1011.               (*mangled)++;
  1012.             }
  1013.         }
  1014.           if (**mangled == 'e') /* exponent */
  1015.         {
  1016.           string_appendn (tname, "e", 1);
  1017.           (*mangled)++;
  1018.           while (isdigit (**mangled))    
  1019.             {
  1020.               string_appendn (tname, *mangled, 1);
  1021.               (*mangled)++;
  1022.             }
  1023.         }
  1024.         }
  1025.       else if (is_pointer)
  1026.         {
  1027.           if (!get_count (mangled, &symbol_len))
  1028.         {
  1029.           success = 0;
  1030.           break;
  1031.         }
  1032.           string_appendn (tname, *mangled, symbol_len);
  1033.           *mangled += symbol_len;
  1034.         }
  1035.     }
  1036.       need_comma = 1;
  1037.     }
  1038.   if (tname->p[-1] == '>')
  1039.     string_append (tname, " ");
  1040.   string_append (tname, ">");
  1041.   
  1042. /*
  1043.       if (work -> static_type)
  1044.     {
  1045.       string_append (declp, *mangled + 1);
  1046.       *mangled += strlen (*mangled);
  1047.       success = 1;
  1048.     }
  1049.       else
  1050.     {
  1051.       success = demangle_args (work, mangled, declp);
  1052.     }
  1053.     }
  1054. */
  1055.   return (success);
  1056. }
  1057.  
  1058. static int
  1059. arm_pt (work, mangled, n, anchor, args)
  1060.      struct work_stuff *work;
  1061.      CONST char *mangled;
  1062.      int n;
  1063.      CONST char **anchor, **args;
  1064. {
  1065.   /* ARM template? */
  1066.   if (ARM_DEMANGLING && (*anchor = strstr(mangled, "__pt__")))
  1067.     {
  1068.     int len;
  1069.         *args = *anchor + 6;
  1070.     len = consume_count (args);
  1071.         if (*args + len == mangled + n && **args == '_')
  1072.       {
  1073.         ++*args;
  1074.         return 1;
  1075.       }
  1076.     }
  1077.   return 0;
  1078. }
  1079.  
  1080. static void
  1081. demangle_arm_pt (work, mangled, n, declp)
  1082.      struct work_stuff *work;
  1083.      CONST char **mangled;
  1084.      int n;
  1085.      string *declp;
  1086. {
  1087.   CONST char *p;
  1088.   CONST char *args;
  1089.   CONST char *e = *mangled + n;
  1090.  
  1091.   /* ARM template? */
  1092.   if (arm_pt (work, *mangled, n, &p, &args))
  1093.   {
  1094.     string arg;
  1095.     string_init (&arg);
  1096.     string_appendn (declp, *mangled, p - *mangled);
  1097.     string_append (declp, "<");
  1098.     /* should do error checking here */
  1099.     while (args < e) {
  1100.       string_clear (&arg);
  1101.       do_type (work, &args, &arg);
  1102.       string_appends (declp, &arg);
  1103.       string_append (declp, ",");
  1104.     }
  1105.     string_delete (&arg);
  1106.     --declp->p;
  1107.     string_append (declp, ">");
  1108.   }
  1109.   else
  1110.   {
  1111.     string_appendn (declp, *mangled, n);
  1112.   }
  1113.   *mangled += n;
  1114. }
  1115.  
  1116. static int
  1117. demangle_class_name (work, mangled, declp)
  1118.      struct work_stuff *work;
  1119.      CONST char **mangled;
  1120.      string *declp;
  1121. {
  1122.   int n;
  1123.   int success = 0;
  1124.  
  1125.   n = consume_count (mangled);
  1126.   if (strlen (*mangled) >= n)
  1127.   {
  1128.     demangle_arm_pt (work, mangled, n, declp);
  1129.     success = 1;
  1130.   }
  1131.  
  1132.   return (success);
  1133. }
  1134.  
  1135. /*
  1136.  
  1137. LOCAL FUNCTION
  1138.  
  1139.     demangle_class -- demangle a mangled class sequence
  1140.  
  1141. SYNOPSIS
  1142.  
  1143.     static int
  1144.     demangle_class (struct work_stuff *work, const char **mangled,
  1145.             strint *declp)
  1146.  
  1147. DESCRIPTION
  1148.  
  1149.     DECLP points to the buffer into which demangling is being done.
  1150.  
  1151.     *MANGLED points to the current token to be demangled.  On input,
  1152.     it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
  1153.     On exit, it points to the next token after the mangled class on
  1154.     success, or the first unconsumed token on failure.
  1155.  
  1156.     If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
  1157.     we are demangling a constructor or destructor.  In this case
  1158.     we prepend "class::class" or "class::~class" to DECLP.
  1159.  
  1160.     Otherwise, we prepend "class::" to the current DECLP.
  1161.  
  1162.     Reset the constructor/destructor flags once they have been
  1163.     "consumed".  This allows demangle_class to be called later during
  1164.     the same demangling, to do normal class demangling.
  1165.  
  1166.     Returns 1 if demangling is successful, 0 otherwise.
  1167.  
  1168. */
  1169.  
  1170. static int
  1171. demangle_class (work, mangled, declp)
  1172.      struct work_stuff *work;
  1173.      CONST char **mangled;
  1174.      string *declp;
  1175. {
  1176.   int success = 0;
  1177.   string class_name;
  1178.  
  1179.   string_init (&class_name);
  1180.   if (demangle_class_name (work, mangled, &class_name))
  1181.     {
  1182.       if ((work->constructor & 1) || (work->destructor & 1))
  1183.     {
  1184.       string_prepends (declp, &class_name);
  1185.       if (work -> destructor & 1)
  1186.         {
  1187.           string_prepend (declp, "~");
  1188.               work -> destructor -= 1;
  1189.         }
  1190.       else
  1191.         {
  1192.           work -> constructor -= 1; 
  1193.         }
  1194.     }
  1195.       string_prepend (declp, "::");
  1196.       string_prepends (declp, &class_name);
  1197.       success = 1;
  1198.     }
  1199.   string_delete (&class_name);
  1200.   return (success);
  1201. }
  1202.  
  1203. /*
  1204.  
  1205. LOCAL FUNCTION
  1206.  
  1207.     demangle_prefix -- consume the mangled name prefix and find signature
  1208.  
  1209. SYNOPSIS
  1210.  
  1211.     static int
  1212.     demangle_prefix (struct work_stuff *work, const char **mangled,
  1213.              string *declp);
  1214.  
  1215. DESCRIPTION
  1216.  
  1217.     Consume and demangle the prefix of the mangled name.
  1218.  
  1219.     DECLP points to the string buffer into which demangled output is
  1220.     placed.  On entry, the buffer is empty.  On exit it contains
  1221.     the root function name, the demangled operator name, or in some
  1222.     special cases either nothing or the completely demangled result.
  1223.  
  1224.     MANGLED points to the current pointer into the mangled name.  As each
  1225.     token of the mangled name is consumed, it is updated.  Upon entry
  1226.     the current mangled name pointer points to the first character of
  1227.     the mangled name.  Upon exit, it should point to the first character
  1228.     of the signature if demangling was successful, or to the first
  1229.     unconsumed character if demangling of the prefix was unsuccessful.
  1230.     
  1231.     Returns 1 on success, 0 otherwise.
  1232.  */
  1233.  
  1234. static int
  1235. demangle_prefix (work, mangled, declp)
  1236.      struct work_stuff *work;
  1237.      CONST char **mangled;
  1238.      string *declp;
  1239. {
  1240.   int success = 1;
  1241.   CONST char *scan;
  1242.   int i;
  1243.  
  1244.   if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
  1245.     {
  1246.       char *marker = strchr (cplus_markers, (*mangled)[8]);
  1247.       if (marker != NULL && *marker == (*mangled)[10])
  1248.     {
  1249.       if ((*mangled)[9] == 'D')
  1250.         {
  1251.           /* it's a GNU global destructor to be executed at program exit */
  1252.           (*mangled) += 11;
  1253.           work->destructor = 2;
  1254.         }
  1255.       else if ((*mangled)[9] == 'I')
  1256.         {
  1257.           /* it's a GNU global constructor to be executed at program init */
  1258.           (*mangled) += 11;
  1259.           work->constructor = 2;
  1260.         }
  1261.     }
  1262.     }
  1263.   else if (ARM_DEMANGLING && strncmp(*mangled, "__std__", 7) == 0)
  1264.     {
  1265.       /* it's a ARM global destructor to be executed at program exit */
  1266.       (*mangled) += 7;
  1267.       work->destructor = 2;
  1268.     }
  1269.   else if (ARM_DEMANGLING && strncmp(*mangled, "__sti__", 7) == 0)
  1270.     {
  1271.       /* it's a ARM global constructor to be executed at program initial */
  1272.       (*mangled) += 7;
  1273.       work->constructor = 2;
  1274.     }
  1275.  
  1276. /*  This block of code is a reduction in strength time optimization
  1277.     of:
  1278.         scan = strstr (*mangled, "__"); */
  1279.  
  1280.   {
  1281.     scan = *mangled;
  1282.  
  1283.     do {
  1284.       scan = strchr (scan, '_');
  1285.     } while (scan != NULL && *++scan != '_');
  1286.  
  1287.     if (scan != NULL) --scan;
  1288.   }
  1289.  
  1290.   if (scan != NULL)
  1291.     {
  1292.       /* We found a sequence of two or more '_', ensure that we start at
  1293.      the last pair in the sequence. */
  1294.       i = strspn (scan, "_");
  1295.       if (i > 2)
  1296.     {
  1297.       scan += (i - 2); 
  1298.     }
  1299.     }
  1300.  
  1301.   if (scan == NULL)
  1302.     {
  1303.       success = 0;
  1304.     }
  1305.   else if (work -> static_type)
  1306.     {
  1307.       if (!isdigit (scan[0]) && (scan[0] != 't'))
  1308.     {
  1309.       success = 0;
  1310.     }
  1311.     }
  1312.   else if ((scan == *mangled) &&
  1313.        (isdigit (scan[2]) || (scan[2] == 'Q') || (scan[2] == 't')))
  1314.     {
  1315.       /* The ARM says nothing about the mangling of local variables.
  1316.      But cfront mangles local variables by prepending __<nesting_level>
  1317.      to them. As an extension to ARM demangling we handle this case.  */
  1318.       if ((LUCID_DEMANGLING || ARM_DEMANGLING) && isdigit (scan[2]))
  1319.     {
  1320.       *mangled = scan + 2;
  1321.       consume_count (mangled);
  1322.       string_append (declp, *mangled);
  1323.       *mangled += strlen (*mangled);
  1324.       success = 1; 
  1325.     }
  1326.       else
  1327.     {
  1328.       /* A GNU style constructor starts with __[0-9Qt].  But cfront uses
  1329.          names like __Q2_3foo3bar for nested type names.  So don't accept
  1330.          this style of constructor for cfront demangling.  */
  1331.       if (!(LUCID_DEMANGLING || ARM_DEMANGLING))
  1332.         work -> constructor += 1;
  1333.       *mangled = scan + 2;
  1334.     }
  1335.     }
  1336.   else if ((scan == *mangled) && !isdigit (scan[2]) && (scan[2] != 't'))
  1337.     {
  1338.       /* Mangled name starts with "__".  Skip over any leading '_' characters,
  1339.      then find the next "__" that separates the prefix from the signature.
  1340.      */
  1341.       if (!(ARM_DEMANGLING || LUCID_DEMANGLING)
  1342.       || (arm_special (work, mangled, declp) == 0))
  1343.     {
  1344.       while (*scan == '_')
  1345.         {
  1346.           scan++;
  1347.         }
  1348.       if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
  1349.         {
  1350.           /* No separator (I.E. "__not_mangled"), or empty signature
  1351.          (I.E. "__not_mangled_either__") */
  1352.           success = 0;
  1353.         }
  1354.       else
  1355.         {
  1356.           demangle_function_name (work, mangled, declp, scan);
  1357.         }
  1358.     }
  1359.     }
  1360.   else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
  1361.     {
  1362.       /* Cfront-style parameterized type.  Handled later as a signature. */
  1363.       success = 1;
  1364.  
  1365.       /* ARM template? */
  1366.       demangle_arm_pt (work, mangled, strlen (*mangled), declp);
  1367.     }
  1368.   else if (*(scan + 2) != '\0')
  1369.     {
  1370.       /* Mangled name does not start with "__" but does have one somewhere
  1371.      in there with non empty stuff after it.  Looks like a global
  1372.      function name. */
  1373.       demangle_function_name (work, mangled, declp, scan);
  1374.     }
  1375.   else
  1376.     {
  1377.       /* Doesn't look like a mangled name */
  1378.       success = 0;
  1379.     }
  1380.  
  1381.   if (!success && (work->constructor == 2 || work->destructor == 2))
  1382.     {
  1383.       string_append (declp, *mangled);
  1384.       *mangled += strlen (*mangled);
  1385.       success = 1;
  1386.     } 
  1387.   return (success);
  1388. }
  1389.  
  1390. /*
  1391.  
  1392. LOCAL FUNCTION
  1393.  
  1394.     gnu_special -- special handling of gnu mangled strings
  1395.  
  1396. SYNOPSIS
  1397.  
  1398.     static int
  1399.     gnu_special (struct work_stuff *work, const char **mangled,
  1400.              string *declp);
  1401.  
  1402.  
  1403. DESCRIPTION
  1404.  
  1405.     Process some special GNU style mangling forms that don't fit
  1406.     the normal pattern.  For example:
  1407.  
  1408.         _$_3foo        (destructor for class foo)
  1409.         _vt$foo        (foo virtual table)
  1410.         _vt$foo$bar    (foo::bar virtual table)
  1411.         __vt_foo    (foo virtual table, new style with thunks)
  1412.         _3foo$varname    (static data member)
  1413.         _Q22rs2tu$vw    (static data member)
  1414.         __t6vector1Zii    (constructor with template)
  1415.         __thunk_4__$_7ostream (virtual function thunk)
  1416.  */
  1417.  
  1418. static int
  1419. gnu_special (work, mangled, declp)
  1420.      struct work_stuff *work;
  1421.      CONST char **mangled;
  1422.      string *declp;
  1423. {
  1424.   int n;
  1425.   int success = 1;
  1426.   CONST char *p;
  1427.  
  1428.   if ((*mangled)[0] == '_'
  1429.       && strchr (cplus_markers, (*mangled)[1]) != NULL
  1430.       && (*mangled)[2] == '_')
  1431.     {
  1432.       /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
  1433.       (*mangled) += 3;
  1434.       work -> destructor += 1;
  1435.     }
  1436.   else if ((*mangled)[0] == '_'
  1437.        && (((*mangled)[1] == '_'
  1438.         && (*mangled)[2] == 'v'
  1439.         && (*mangled)[3] == 't'
  1440.         && (*mangled)[4] == '_')
  1441.          || ((*mangled)[1] == 'v'
  1442.          && (*mangled)[2] == 't'
  1443.          && strchr (cplus_markers, (*mangled)[3]) != NULL)))
  1444.     {
  1445.       /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
  1446.          and create the decl.  Note that we consume the entire mangled
  1447.      input string, which means that demangle_signature has no work
  1448.      to do. */
  1449.       if ((*mangled)[2] == 'v')
  1450.     (*mangled) += 5; /* New style, with thunks: "__vt_" */
  1451.       else
  1452.     (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
  1453.       while (**mangled != '\0')
  1454.     {
  1455.       p = strpbrk (*mangled, cplus_markers);
  1456.       switch (**mangled)
  1457.         {
  1458.         case 'Q':
  1459.           success = demangle_qualified (work, mangled, declp, 0, 1);
  1460.           break;
  1461.         case 't':
  1462.           success = demangle_template (work, mangled, declp, 0);
  1463.           break;
  1464.         default:
  1465.           if (isdigit(*mangled[0]))
  1466.         {
  1467.           n = consume_count(mangled);
  1468.         }
  1469.           else
  1470.         {
  1471.           n = strcspn (*mangled, cplus_markers);
  1472.         }
  1473.           string_appendn (declp, *mangled, n);
  1474.           (*mangled) += n;
  1475.         }
  1476.  
  1477.       if (success && ((p == NULL) || (p == *mangled)))
  1478.         {
  1479.           if (p != NULL)
  1480.         {
  1481.           string_append (declp, "::");
  1482.           (*mangled)++;
  1483.         }
  1484.         }
  1485.       else
  1486.         {
  1487.           success = 0;
  1488.           break;
  1489.         }
  1490.     }
  1491.       if (success)
  1492.     string_append (declp, " virtual table");
  1493.     }
  1494.   else if ((*mangled)[0] == '_'
  1495.        && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
  1496.        && (p = strpbrk (*mangled, cplus_markers)) != NULL)
  1497.     {
  1498.       /* static data member, "_3foo$varname" for example */
  1499.       (*mangled)++;
  1500.       switch (**mangled)
  1501.     {
  1502.       case 'Q':
  1503.         success = demangle_qualified (work, mangled, declp, 0, 1);
  1504.         break;
  1505.       case 't':
  1506.         success = demangle_template (work, mangled, declp, 0);
  1507.         break;
  1508.       default:
  1509.         n = consume_count (mangled);
  1510.         string_appendn (declp, *mangled, n);
  1511.         (*mangled) += n;
  1512.     }
  1513.       if (success && (p == *mangled))
  1514.     {
  1515.       /* Consumed everything up to the cplus_marker, append the
  1516.          variable name. */
  1517.       (*mangled)++;
  1518.       string_append (declp, "::");
  1519.       n = strlen (*mangled);
  1520.       string_appendn (declp, *mangled, n);
  1521.       (*mangled) += n;
  1522.     }
  1523.       else
  1524.     {
  1525.       success = 0;
  1526.     }
  1527.     }
  1528.   else if (strncmp (*mangled, "__thunk_", 8) == 0)
  1529.     {
  1530.       int delta = ((*mangled) += 8, consume_count (mangled));
  1531.       char *method = cplus_demangle (++*mangled, work->options);
  1532.       if (method)
  1533.     {
  1534.       char buf[50];
  1535.       sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
  1536.       string_append (declp, buf);
  1537.       string_append (declp, method);
  1538.       free (method);
  1539.       n = strlen (*mangled);
  1540.       (*mangled) += n;
  1541.     }
  1542.       else
  1543.     {
  1544.       success = 0;
  1545.     }
  1546.     }
  1547.   else
  1548.     {
  1549.       success = 0;
  1550.     }
  1551.   return (success);
  1552. }
  1553.  
  1554. /*
  1555.  
  1556. LOCAL FUNCTION
  1557.  
  1558.     arm_special -- special handling of ARM/lucid mangled strings
  1559.  
  1560. SYNOPSIS
  1561.  
  1562.     static int
  1563.     arm_special (struct work_stuff *work, const char **mangled,
  1564.             string *declp);
  1565.  
  1566.  
  1567. DESCRIPTION
  1568.  
  1569.     Process some special ARM style mangling forms that don't fit
  1570.     the normal pattern.  For example:
  1571.  
  1572.         __vtbl__3foo        (foo virtual table)
  1573.         __vtbl__3foo__3bar    (bar::foo virtual table)
  1574.  
  1575.  */
  1576.  
  1577. static int
  1578. arm_special (work, mangled, declp)
  1579.      struct work_stuff *work;
  1580.      CONST char **mangled;
  1581.      string *declp;
  1582. {
  1583.   int n;
  1584.   int success = 1;
  1585.   CONST char *scan;
  1586.  
  1587.   if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
  1588.     {
  1589.       /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
  1590.          and create the decl.  Note that we consume the entire mangled
  1591.      input string, which means that demangle_signature has no work
  1592.      to do. */
  1593.       scan = *mangled + ARM_VTABLE_STRLEN;
  1594.       while (*scan != '\0')        /* first check it can be demangled */
  1595.         {
  1596.           n = consume_count (&scan);
  1597.           if (n==0)
  1598.         {
  1599.           return (0);           /* no good */
  1600.         }
  1601.           scan += n;
  1602.           if (scan[0] == '_' && scan[1] == '_')
  1603.         {
  1604.           scan += 2;
  1605.         }
  1606.         }
  1607.       (*mangled) += ARM_VTABLE_STRLEN;
  1608.       while (**mangled != '\0')
  1609.     {
  1610.       n = consume_count (mangled);
  1611.       string_prependn (declp, *mangled, n);
  1612.       (*mangled) += n;
  1613.       if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
  1614.         {
  1615.           string_prepend (declp, "::");
  1616.           (*mangled) += 2;
  1617.         }
  1618.     }
  1619.       string_append (declp, " virtual table");
  1620.     }
  1621.   else
  1622.     {
  1623.       success = 0;
  1624.     }
  1625.   return (success);
  1626. }
  1627.  
  1628. /*
  1629.  
  1630. LOCAL FUNCTION
  1631.  
  1632.     demangle_qualified -- demangle 'Q' qualified name strings
  1633.  
  1634. SYNOPSIS
  1635.  
  1636.     static int
  1637.     demangle_qualified (struct work_stuff *, const char *mangled,
  1638.                 string *result, int isfuncname, int append);
  1639.  
  1640. DESCRIPTION
  1641.  
  1642.     Demangle a qualified name, such as "Q25Outer5Inner" which is
  1643.     the mangled form of "Outer::Inner".  The demangled output is
  1644.     prepended or appended to the result string according to the
  1645.     state of the append flag.
  1646.  
  1647.     If isfuncname is nonzero, then the qualified name we are building
  1648.     is going to be used as a member function name, so if it is a
  1649.     constructor or destructor function, append an appropriate
  1650.     constructor or destructor name.  I.E. for the above example,
  1651.     the result for use as a constructor is "Outer::Inner::Inner"
  1652.     and the result for use as a destructor is "Outer::Inner::~Inner".
  1653.  
  1654. BUGS
  1655.  
  1656.     Numeric conversion is ASCII dependent (FIXME).
  1657.  
  1658.  */
  1659.  
  1660. static int
  1661. demangle_qualified (work, mangled, result, isfuncname, append)
  1662.      struct work_stuff *work;
  1663.      CONST char **mangled;
  1664.      string *result;
  1665.      int isfuncname;
  1666.      int append;
  1667. {
  1668.   int qualifiers;
  1669.   int namelength;
  1670.   int success = 1;
  1671.   CONST char *p;
  1672.   char num[2];
  1673.   string temp;
  1674.  
  1675.   string_init (&temp);
  1676.   switch ((*mangled)[1])
  1677.     {
  1678.     case '_':
  1679.       /* GNU mangled name with more than 9 classes.  The count is preceded
  1680.      by an underscore (to distinguish it from the <= 9 case) and followed
  1681.      by an underscore.  */
  1682.       p = *mangled + 2;
  1683.       qualifiers = atoi (p);
  1684.       if (!isdigit (*p) || *p == '0')
  1685.     success = 0;
  1686.  
  1687.       /* Skip the digits.  */
  1688.       while (isdigit (*p))
  1689.     ++p;
  1690.  
  1691.       if (*p != '_')
  1692.     success = 0;
  1693.  
  1694.       *mangled = p + 1;
  1695.       break;
  1696.  
  1697.     case '1':
  1698.     case '2':
  1699.     case '3':
  1700.     case '4':
  1701.     case '5':
  1702.     case '6':
  1703.     case '7':
  1704.     case '8':
  1705.     case '9':
  1706.       /* The count is in a single digit.  */
  1707.       num[0] = (*mangled)[1];
  1708.       num[1] = '\0';
  1709.       qualifiers = atoi (num);
  1710.  
  1711.       /* If there is an underscore after the digit, skip it.  This is
  1712.      said to be for ARM-qualified names, but the ARM makes no
  1713.      mention of such an underscore.  Perhaps cfront uses one.  */
  1714.       if ((*mangled)[2] == '_')
  1715.     {
  1716.       (*mangled)++;
  1717.     }
  1718.       (*mangled) += 2;
  1719.       break;
  1720.  
  1721.     case '0':
  1722.     default:
  1723.       success = 0;
  1724.     }
  1725.  
  1726.   if (!success)
  1727.     return success;
  1728.  
  1729.   /* Pick off the names and collect them in the temp buffer in the order
  1730.      in which they are found, separated by '::'. */
  1731.  
  1732.   while (qualifiers-- > 0)
  1733.     {
  1734.       if (*mangled[0] == '_') 
  1735.     *mangled = *mangled + 1;
  1736.       if (*mangled[0] == 't')
  1737.     {
  1738.       success = demangle_template(work, mangled, &temp, 0);
  1739.       if (!success) break;
  1740.     }
  1741.       else
  1742.         {    
  1743.       namelength = consume_count (mangled);
  1744.             if (strlen (*mangled) < namelength)
  1745.         {
  1746.         /* Simple sanity check failed */
  1747.            success = 0;
  1748.            break;
  1749.         }
  1750.             string_appendn (&temp, *mangled, namelength);
  1751.             *mangled += namelength;
  1752.     }
  1753.       if (qualifiers > 0)
  1754.         {
  1755.           string_appendn (&temp, "::", 2);
  1756.         }
  1757.     }
  1758.  
  1759.   /* If we are using the result as a function name, we need to append
  1760.      the appropriate '::' separated constructor or destructor name.
  1761.      We do this here because this is the most convenient place, where
  1762.      we already have a pointer to the name and the length of the name. */
  1763.  
  1764.   if (isfuncname && (work->constructor & 1 || work->destructor & 1))
  1765.     {
  1766.       string_appendn (&temp, "::", 2);
  1767.       if (work -> destructor & 1)
  1768.     {
  1769.       string_append (&temp, "~");
  1770.     }
  1771.       string_appendn (&temp, (*mangled) - namelength, namelength);
  1772.     }
  1773.  
  1774.   /* Now either prepend the temp buffer to the result, or append it, 
  1775.      depending upon the state of the append flag. */
  1776.  
  1777.   if (append)
  1778.     {
  1779.       string_appends (result, &temp);
  1780.     }
  1781.   else
  1782.     {
  1783.       if (!STRING_EMPTY (result))
  1784.     {
  1785.       string_appendn (&temp, "::", 2);
  1786.     }
  1787.       string_prepends (result, &temp);
  1788.     }
  1789.  
  1790.   string_delete (&temp);
  1791.   return (success);
  1792. }
  1793.  
  1794. /*
  1795.  
  1796. LOCAL FUNCTION
  1797.  
  1798.     get_count -- convert an ascii count to integer, consuming tokens
  1799.  
  1800. SYNOPSIS
  1801.  
  1802.     static int
  1803.     get_count (const char **type, int *count)
  1804.  
  1805. DESCRIPTION
  1806.  
  1807.     Return 0 if no conversion is performed, 1 if a string is converted.
  1808. */
  1809.  
  1810. static int
  1811. get_count (type, count)
  1812.      CONST char **type;
  1813.      int *count;
  1814. {
  1815.   CONST char *p;
  1816.   int n;
  1817.  
  1818.   if (!isdigit (**type))
  1819.     {
  1820.       return (0);
  1821.     }
  1822.   else
  1823.     {
  1824.       *count = **type - '0';
  1825.       (*type)++;
  1826.       if (isdigit (**type))
  1827.     {
  1828.       p = *type;
  1829.       n = *count;
  1830.       do 
  1831.         {
  1832.           n *= 10;
  1833.           n += *p - '0';
  1834.           p++;
  1835.         } 
  1836.       while (isdigit (*p));
  1837.       if (*p == '_')
  1838.         {
  1839.           *type = p + 1;
  1840.           *count = n;
  1841.         }
  1842.     }
  1843.     }
  1844.   return (1);
  1845. }
  1846.  
  1847. /* result will be initialised here; it will be freed on failure */
  1848.  
  1849. static int
  1850. do_type (work, mangled, result)
  1851.      struct work_stuff *work;
  1852.      CONST char **mangled;
  1853.      string *result;
  1854. {
  1855.   int n;
  1856.   int done;
  1857.   int success;
  1858.   string decl;
  1859.   CONST char *remembered_type;
  1860.   int constp;
  1861.   int volatilep;
  1862.  
  1863.   string_init (&decl);
  1864.   string_init (result);
  1865.  
  1866.   done = 0;
  1867.   success = 1;
  1868.   while (success && !done)
  1869.     {
  1870.       int member;
  1871.       switch (**mangled)
  1872.     {
  1873.  
  1874.     /* A pointer type */
  1875.     case 'P':
  1876.     case 'p':
  1877.       (*mangled)++;
  1878.       string_prepend (&decl, "*");
  1879.       break;
  1880.  
  1881.     /* A reference type */
  1882.     case 'R':
  1883.       (*mangled)++;
  1884.       string_prepend (&decl, "&");
  1885.       break;
  1886.  
  1887.     /* An array */
  1888.     case 'A':
  1889.       {
  1890.         CONST char *p = ++(*mangled);
  1891.  
  1892.         string_prepend (&decl, "(");
  1893.         string_append (&decl, ")[");
  1894.         /* Copy anything up until the next underscore (the size of the
  1895.            array).  */
  1896.         while (**mangled && **mangled != '_')
  1897.           ++(*mangled);
  1898.         if (**mangled == '_')
  1899.           {
  1900.         string_appendn (&decl, p, *mangled - p);
  1901.         string_append (&decl, "]");             
  1902.         *mangled += 1;
  1903.           }
  1904.         else
  1905.           success = 0;
  1906.         break;
  1907.       }
  1908.  
  1909.     /* A back reference to a previously seen type */
  1910.     case 'T':
  1911.       (*mangled)++;
  1912.       if (!get_count (mangled, &n) || n >= work -> ntypes)
  1913.         {
  1914.           success = 0;
  1915.         }
  1916.       else
  1917.         {
  1918.           remembered_type = work -> typevec[n];
  1919.           mangled = &remembered_type;
  1920.         }
  1921.       break;
  1922.  
  1923.     /* A function */
  1924.     case 'F':
  1925.       (*mangled)++;
  1926.       if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
  1927.         {
  1928.           string_prepend (&decl, "(");
  1929.           string_append (&decl, ")");
  1930.         }
  1931.       /* After picking off the function args, we expect to either find the
  1932.          function return type (preceded by an '_') or the end of the
  1933.          string. */
  1934.       if (!demangle_args (work, mangled, &decl)
  1935.           || (**mangled != '_' && **mangled != '\0'))
  1936.         {
  1937.           success = 0;
  1938.         }
  1939.       if (success && (**mangled == '_'))
  1940.         {
  1941.           (*mangled)++;
  1942.         }
  1943.       break;
  1944.  
  1945.     case 'M':
  1946.     case 'O':
  1947.       {
  1948.         constp = 0;
  1949.         volatilep = 0;
  1950.  
  1951.         member = **mangled == 'M';
  1952.         (*mangled)++;
  1953.         if (!isdigit (**mangled))
  1954.           {
  1955.         success = 0;
  1956.         break;
  1957.           }
  1958.         n = consume_count (mangled);
  1959.         if (strlen (*mangled) < n)
  1960.           {
  1961.         success = 0;
  1962.         break;
  1963.           }
  1964.         string_append (&decl, ")");
  1965.         string_prepend (&decl, "::");
  1966.         string_prependn (&decl, *mangled, n);
  1967.         string_prepend (&decl, "(");
  1968.         *mangled += n;
  1969.         if (member)
  1970.           {
  1971.         if (**mangled == 'C')
  1972.           {
  1973.             (*mangled)++;
  1974.             constp = 1;
  1975.           }
  1976.         if (**mangled == 'V')
  1977.           {
  1978.             (*mangled)++;
  1979.             volatilep = 1;
  1980.           }
  1981.         if (*(*mangled)++ != 'F')
  1982.           {
  1983.             success = 0;
  1984.             break;
  1985.           }
  1986.           }
  1987.         if ((member && !demangle_args (work, mangled, &decl))
  1988.         || **mangled != '_')
  1989.           {
  1990.         success = 0;
  1991.         break;
  1992.           }
  1993.         (*mangled)++;
  1994.         if (! PRINT_ANSI_QUALIFIERS)
  1995.           {
  1996.         break;
  1997.           }
  1998.         if (constp)
  1999.           {
  2000.         APPEND_BLANK (&decl);
  2001.         string_append (&decl, "const");
  2002.           }
  2003.         if (volatilep)
  2004.           {
  2005.         APPEND_BLANK (&decl);
  2006.         string_append (&decl, "volatile");
  2007.           }
  2008.         break;
  2009.       }
  2010.         case 'G':
  2011.         (*mangled)++;
  2012.         break;
  2013.  
  2014.     case 'C':
  2015.       (*mangled)++;
  2016. /*
  2017.       if ((*mangled)[1] == 'P')
  2018.         {
  2019. */
  2020.           if (PRINT_ANSI_QUALIFIERS)
  2021.         {
  2022.           if (!STRING_EMPTY (&decl))
  2023.             {
  2024.               string_prepend (&decl, " ");
  2025.             }
  2026.           string_prepend (&decl, "const");
  2027.         }
  2028.           break;
  2029. /*
  2030.         }
  2031. */
  2032.  
  2033.       /* fall through */
  2034.     default:
  2035.       done = 1;
  2036.       break;
  2037.     }
  2038.     }
  2039.  
  2040.   switch (**mangled)
  2041.     {
  2042.       /* A qualified name, such as "Outer::Inner". */
  2043.       case 'Q':
  2044.         success = demangle_qualified (work, mangled, result, 0, 1);
  2045.     break;
  2046.  
  2047.       default:
  2048.     success = demangle_fund_type (work, mangled, result);
  2049.     break;
  2050.     }
  2051.  
  2052.   if (success)
  2053.     {
  2054.       if (!STRING_EMPTY (&decl))
  2055.     {
  2056.       string_append (result, " ");
  2057.       string_appends (result, &decl);
  2058.     }
  2059.     }
  2060.   else
  2061.     {
  2062.       string_delete (result);
  2063.     }
  2064.   string_delete (&decl);
  2065.   return (success);
  2066. }
  2067.  
  2068. /* Given a pointer to a type string that represents a fundamental type
  2069.    argument (int, long, unsigned int, etc) in TYPE, a pointer to the
  2070.    string in which the demangled output is being built in RESULT, and
  2071.    the WORK structure, decode the types and add them to the result.
  2072.  
  2073.    For example:
  2074.  
  2075.        "Ci"    =>    "const int"
  2076.     "Sl"    =>    "signed long"
  2077.     "CUs"    =>    "const unsigned short"
  2078.  
  2079.    */
  2080.  
  2081. static int
  2082. demangle_fund_type (work, mangled, result)
  2083.      struct work_stuff *work;
  2084.      CONST char **mangled;
  2085.      string *result;
  2086. {
  2087.   int done = 0;
  2088.   int success = 1;
  2089.  
  2090.   /* First pick off any type qualifiers.  There can be more than one. */
  2091.  
  2092.   while (!done)
  2093.     {
  2094.       switch (**mangled)
  2095.     {
  2096.       case 'C':
  2097.         (*mangled)++;
  2098.         if (PRINT_ANSI_QUALIFIERS)
  2099.           {
  2100.         APPEND_BLANK (result);
  2101.         string_append (result, "const");
  2102.           }
  2103.         break;
  2104.       case 'U':
  2105.         (*mangled)++;
  2106.         APPEND_BLANK (result);
  2107.         string_append (result, "unsigned");
  2108.         break;
  2109.       case 'S': /* signed char only */
  2110.         (*mangled)++;
  2111.         APPEND_BLANK (result);
  2112.         string_append (result, "signed");
  2113.         break;
  2114.       case 'V':
  2115.         (*mangled)++;
  2116.         if (PRINT_ANSI_QUALIFIERS)
  2117.           {
  2118.         APPEND_BLANK (result);
  2119.         string_append (result, "volatile");
  2120.           }
  2121.         break;
  2122.       default:
  2123.         done = 1;
  2124.         break;
  2125.     }
  2126.     }
  2127.  
  2128.   /* Now pick off the fundamental type.  There can be only one. */
  2129.  
  2130.   switch (**mangled)
  2131.     {
  2132.       case '\0':
  2133.       case '_':
  2134.     break;
  2135.       case 'v':
  2136.     (*mangled)++;
  2137.     APPEND_BLANK (result);
  2138.     string_append (result, "void");
  2139.     break;
  2140.       case 'x':
  2141.     (*mangled)++;
  2142.     APPEND_BLANK (result);
  2143.     string_append (result, "long long");
  2144.     break;
  2145.       case 'l':
  2146.     (*mangled)++;
  2147.     APPEND_BLANK (result);
  2148.     string_append (result, "long");
  2149.     break;
  2150.       case 'i':
  2151.     (*mangled)++;
  2152.     APPEND_BLANK (result);
  2153.     string_append (result, "int");
  2154.     break;
  2155.       case 's':
  2156.     (*mangled)++;
  2157.     APPEND_BLANK (result);
  2158.     string_append (result, "short");
  2159.     break;
  2160.       case 'b':
  2161.     (*mangled)++;
  2162.     APPEND_BLANK (result);
  2163.     string_append (result, "bool");
  2164.     break;
  2165.       case 'c':
  2166.     (*mangled)++;
  2167.     APPEND_BLANK (result);
  2168.     string_append (result, "char");
  2169.     break;
  2170.       case 'w':
  2171.     (*mangled)++;
  2172.     APPEND_BLANK (result);
  2173.     string_append (result, "wchar_t");
  2174.     break;
  2175.       case 'r':
  2176.     (*mangled)++;
  2177.     APPEND_BLANK (result);
  2178.     string_append (result, "long double");
  2179.     break;
  2180.       case 'd':
  2181.     (*mangled)++;
  2182.     APPEND_BLANK (result);
  2183.     string_append (result, "double");
  2184.     break;
  2185.       case 'f':
  2186.     (*mangled)++;
  2187.     APPEND_BLANK (result);
  2188.     string_append (result, "float");
  2189.     break;
  2190.       case 'G':
  2191.     (*mangled)++;
  2192.     if (!isdigit (**mangled))
  2193.       {
  2194.         success = 0;
  2195.         break;
  2196.       }
  2197.     /* fall through */
  2198.       /* An explicit type, such as "6mytype" or "7integer" */
  2199.       case '0':
  2200.       case '1':
  2201.       case '2':
  2202.       case '3':
  2203.       case '4':
  2204.       case '5':
  2205.       case '6':
  2206.       case '7':
  2207.       case '8':
  2208.       case '9':
  2209.     APPEND_BLANK (result);
  2210.     if (!demangle_class_name (work, mangled, result)) {
  2211.       --result->p;
  2212.       success = 0;
  2213.     }
  2214.     break;
  2215.       case 't':
  2216.         success = demangle_template(work,mangled, result, 0);
  2217.         break;
  2218.       default:
  2219.     success = 0;
  2220.     break;
  2221.       }
  2222.  
  2223.   return (success);
  2224. }
  2225.  
  2226. /* `result' will be initialized in do_type; it will be freed on failure */
  2227.  
  2228. static int
  2229. do_arg (work, mangled, result)
  2230.      struct work_stuff *work;
  2231.      CONST char **mangled;
  2232.      string *result;
  2233. {
  2234.   CONST char *start = *mangled;
  2235.  
  2236.   if (!do_type (work, mangled, result))
  2237.     {
  2238.       return (0);
  2239.     }
  2240.   else
  2241.     {
  2242.       remember_type (work, start, *mangled - start);
  2243.       return (1);
  2244.     }
  2245. }
  2246.  
  2247. static void
  2248. remember_type (work, start, len)
  2249.      struct work_stuff *work;
  2250.      CONST char *start;
  2251.      int len;
  2252. {
  2253.   char *tem;
  2254.  
  2255.   if (work -> ntypes >= work -> typevec_size)
  2256.     {
  2257.       if (work -> typevec_size == 0)
  2258.     {
  2259.       work -> typevec_size = 3;
  2260.       work -> typevec =
  2261.         (char **) xmalloc (sizeof (char *) * work -> typevec_size);
  2262.     }
  2263.       else
  2264.     {
  2265.       work -> typevec_size *= 2;
  2266.       work -> typevec =
  2267.         (char **) xrealloc ((char *)work -> typevec,
  2268.                 sizeof (char *) * work -> typevec_size);
  2269.     }
  2270.     }
  2271.   tem = xmalloc (len + 1);
  2272.   memcpy (tem, start, len);
  2273.   tem[len] = '\0';
  2274.   work -> typevec[work -> ntypes++] = tem;
  2275. }
  2276.  
  2277. /* Forget the remembered types, but not the type vector itself. */
  2278.  
  2279. static void
  2280. forget_types (work)
  2281.      struct work_stuff *work;
  2282. {
  2283.   int i;
  2284.  
  2285.   while (work -> ntypes > 0)
  2286.     {
  2287.       i = --(work -> ntypes);
  2288.       if (work -> typevec[i] != NULL)
  2289.     {
  2290.       free (work -> typevec[i]);
  2291.       work -> typevec[i] = NULL;
  2292.     }
  2293.     }
  2294. }
  2295.  
  2296. /* Process the argument list part of the signature, after any class spec
  2297.    has been consumed, as well as the first 'F' character (if any).  For
  2298.    example:
  2299.  
  2300.    "__als__3fooRT0"        =>    process "RT0"
  2301.    "complexfunc5__FPFPc_PFl_i"    =>    process "PFPc_PFl_i"
  2302.  
  2303.    DECLP must be already initialised, usually non-empty.  It won't be freed
  2304.    on failure.
  2305.  
  2306.    Note that g++ differs significantly from ARM and lucid style mangling
  2307.    with regards to references to previously seen types.  For example, given
  2308.    the source fragment:
  2309.  
  2310.      class foo {
  2311.        public:
  2312.        foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
  2313.      };
  2314.  
  2315.      foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  2316.      void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  2317.  
  2318.    g++ produces the names:
  2319.  
  2320.      __3fooiRT0iT2iT2
  2321.      foo__FiR3fooiT1iT1
  2322.  
  2323.    while lcc (and presumably other ARM style compilers as well) produces:
  2324.  
  2325.      foo__FiR3fooT1T2T1T2
  2326.      __ct__3fooFiR3fooT1T2T1T2
  2327.  
  2328.    Note that g++ bases it's type numbers starting at zero and counts all
  2329.    previously seen types, while lucid/ARM bases it's type numbers starting
  2330.    at one and only considers types after it has seen the 'F' character
  2331.    indicating the start of the function args.  For lucid/ARM style, we
  2332.    account for this difference by discarding any previously seen types when
  2333.    we see the 'F' character, and subtracting one from the type number
  2334.    reference.
  2335.  
  2336.  */
  2337.  
  2338. static int
  2339. demangle_args (work, mangled, declp)
  2340.      struct work_stuff *work;
  2341.      CONST char **mangled;
  2342.      string *declp;
  2343. {
  2344.   string arg;
  2345.   int need_comma = 0;
  2346.   int r;
  2347.   int t;
  2348.   CONST char *tem;
  2349.   char temptype;
  2350.  
  2351.   if (PRINT_ARG_TYPES)
  2352.     {
  2353.       string_append (declp, "(");
  2354.       if (**mangled == '\0')
  2355.     {
  2356.       string_append (declp, "void");
  2357.     }
  2358.     }
  2359.  
  2360.   while (**mangled != '_' && **mangled != '\0' && **mangled != 'e')
  2361.     {
  2362.       if ((**mangled == 'N') || (**mangled == 'T'))
  2363.     {
  2364.       temptype = *(*mangled)++;
  2365.       
  2366.       if (temptype == 'N')
  2367.         {
  2368.           if (!get_count (mangled, &r))
  2369.         {
  2370.           return (0);
  2371.         }
  2372.         }
  2373.       else
  2374.         {
  2375.           r = 1;
  2376.         }
  2377.           if (ARM_DEMANGLING && work -> ntypes >= 10)
  2378.             {
  2379.               /* If we have 10 or more types we might have more than a 1 digit
  2380.                  index so we'll have to consume the whole count here. This
  2381.                  will loose if the next thing is a type name preceeded by a
  2382.                  count but it's impossible to demangle that case properly
  2383.                  anyway. Eg if we already have 12 types is T12Pc "(..., type1,
  2384.                  anyway. Eg if we already have 12 types is T12Pc "(..., type1,
  2385.                  Pc, ...)"  or "(..., type12, char *, ...)" */
  2386.               if ((t = consume_count(mangled)) == 0)
  2387.                 {
  2388.                   return (0);
  2389.                 }
  2390.             }
  2391.           else
  2392.         {
  2393.           if (!get_count (mangled, &t))
  2394.             {
  2395.               return (0);
  2396.             }
  2397.         }
  2398.       if (LUCID_DEMANGLING || ARM_DEMANGLING)
  2399.         {
  2400.           t--;
  2401.         }
  2402.       /* Validate the type index.  Protect against illegal indices from
  2403.          malformed type strings. */
  2404.       if ((t < 0) || (t >= work -> ntypes))
  2405.         {
  2406.           return (0);
  2407.         }
  2408.       while (--r >= 0)
  2409.         {
  2410.           tem = work -> typevec[t];
  2411.           if (need_comma && PRINT_ARG_TYPES)
  2412.         {
  2413.           string_append (declp, ", ");
  2414.         }
  2415.           if (!do_arg (work, &tem, &arg))
  2416.         {
  2417.           return (0);
  2418.         }
  2419.           if (PRINT_ARG_TYPES)
  2420.         {
  2421.           string_appends (declp, &arg);
  2422.         }
  2423.           string_delete (&arg);
  2424.           need_comma = 1;
  2425.         }
  2426.     }
  2427.       else
  2428.     {
  2429.       if (need_comma & PRINT_ARG_TYPES)
  2430.         {
  2431.           string_append (declp, ", ");
  2432.         }
  2433.       if (!do_arg (work, mangled, &arg))
  2434.         {
  2435.           return (0);
  2436.         }
  2437.       if (PRINT_ARG_TYPES)
  2438.         {
  2439.           string_appends (declp, &arg);
  2440.         }
  2441.       string_delete (&arg);
  2442.       need_comma = 1;
  2443.     }
  2444.     }
  2445.  
  2446.   if (**mangled == 'e')
  2447.     {
  2448.       (*mangled)++;
  2449.       if (PRINT_ARG_TYPES)
  2450.     {
  2451.       if (need_comma)
  2452.         {
  2453.           string_append (declp, ",");
  2454.         }
  2455.       string_append (declp, "...");
  2456.     }
  2457.     }
  2458.  
  2459.   if (PRINT_ARG_TYPES)
  2460.     {
  2461.       string_append (declp, ")");
  2462.     }
  2463.   return (1);
  2464. }
  2465.  
  2466. static void
  2467. demangle_function_name (work, mangled, declp, scan)
  2468.      struct work_stuff *work;
  2469.      CONST char **mangled;
  2470.      string *declp;
  2471.      CONST char *scan;
  2472. {
  2473.   int i;
  2474.   int len;
  2475.   string type;
  2476.   CONST char *tem;
  2477.  
  2478.   string_appendn (declp, (*mangled), scan - (*mangled));
  2479.   string_need (declp, 1);
  2480.   *(declp -> p) = '\0';
  2481.  
  2482.   /* Consume the function name, including the "__" separating the name
  2483.      from the signature.  We are guaranteed that SCAN points to the
  2484.      separator. */
  2485.  
  2486.   (*mangled) = scan + 2;
  2487.  
  2488.   if (LUCID_DEMANGLING || ARM_DEMANGLING)
  2489.     {
  2490.  
  2491.       /* See if we have an ARM style constructor or destructor operator.
  2492.      If so, then just record it, clear the decl, and return.
  2493.      We can't build the actual constructor/destructor decl until later,
  2494.      when we recover the class name from the signature. */
  2495.  
  2496.       if (strcmp (declp -> b, "__ct") == 0)
  2497.     {
  2498.       work -> constructor += 1;
  2499.       string_clear (declp);
  2500.       return;
  2501.     }
  2502.       else if (strcmp (declp -> b, "__dt") == 0)
  2503.     {
  2504.       work -> destructor += 1;
  2505.       string_clear (declp);
  2506.       return;
  2507.     }
  2508.     }
  2509.  
  2510.   if (declp->p - declp->b >= 3 
  2511.       && declp->b[0] == 'o'
  2512.       && declp->b[1] == 'p'
  2513.       && strchr (cplus_markers, declp->b[2]) != NULL)
  2514.     {
  2515.       /* see if it's an assignment expression */
  2516.       if (declp->p - declp->b >= 10 /* op$assign_ */
  2517.       && memcmp (declp->b + 3, "assign_", 7) == 0)
  2518.     {
  2519.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2520.         {
  2521.           len = declp->p - declp->b - 10;
  2522.           if (strlen (optable[i].in) == len
  2523.           && memcmp (optable[i].in, declp->b + 10, len) == 0)
  2524.         {
  2525.           string_clear (declp);
  2526.           string_append (declp, "operator");
  2527.           string_append (declp, optable[i].out);
  2528.           string_append (declp, "=");
  2529.           break;
  2530.         }
  2531.         }
  2532.     }
  2533.       else
  2534.     {
  2535.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2536.         {
  2537.           int len = declp->p - declp->b - 3;
  2538.           if (strlen (optable[i].in) == len 
  2539.           && memcmp (optable[i].in, declp->b + 3, len) == 0)
  2540.         {
  2541.           string_clear (declp);
  2542.           string_append (declp, "operator");
  2543.           string_append (declp, optable[i].out);
  2544.           break;
  2545.         }
  2546.         }
  2547.     }
  2548.     }
  2549.   else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
  2550.        && strchr (cplus_markers, declp->b[4]) != NULL)
  2551.     {
  2552.       /* type conversion operator */
  2553.       tem = declp->b + 5;
  2554.       if (do_type (work, &tem, &type))
  2555.     {
  2556.       string_clear (declp);
  2557.       string_append (declp, "operator ");
  2558.       string_appends (declp, &type);
  2559.       string_delete (&type);
  2560.     }
  2561.     }
  2562.   else if (declp->b[0] == '_' && declp->b[1] == '_'
  2563.       && declp->b[2] == 'o' && declp->b[3] == 'p')
  2564.     {
  2565.       /* ANSI.  */
  2566.       /* type conversion operator.  */
  2567.       tem = declp->b + 4;
  2568.       if (do_type (work, &tem, &type))
  2569.     {
  2570.       string_clear (declp);
  2571.       string_append (declp, "operator ");
  2572.       string_appends (declp, &type);
  2573.       string_delete (&type);
  2574.     }
  2575.     }
  2576.   else if (declp->b[0] == '_' && declp->b[1] == '_'
  2577.        && declp->b[2] >= 'a' && declp->b[2] <= 'z'
  2578.        && declp->b[3] >= 'a' && declp->b[3] <= 'z')
  2579.     {
  2580.       if (declp->b[4] == '\0')
  2581.     {
  2582.       /* Operator.  */
  2583.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2584.         {
  2585.           if (strlen (optable[i].in) == 2
  2586.           && memcmp (optable[i].in, declp->b + 2, 2) == 0)
  2587.         {
  2588.           string_clear (declp);
  2589.           string_append (declp, "operator");
  2590.           string_append (declp, optable[i].out);
  2591.           break;
  2592.         }
  2593.         }
  2594.     }
  2595.       else
  2596.     {
  2597.       if (declp->b[2] == 'a' && declp->b[5] == '\0')
  2598.         {
  2599.           /* Assignment. */
  2600.           for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2601.         {
  2602.           if (strlen (optable[i].in) == 3
  2603.               && memcmp (optable[i].in, declp->b + 2, 3) == 0)
  2604.             {
  2605.               string_clear (declp);
  2606.               string_append (declp, "operator");
  2607.               string_append (declp, optable[i].out);
  2608.               break;
  2609.             }              
  2610.         }
  2611.         }
  2612.     }
  2613.     }
  2614. }
  2615.  
  2616. /* a mini string-handling package */
  2617.  
  2618. static void
  2619. string_need (s, n)
  2620.      string *s;
  2621.      int n;
  2622. {
  2623.   int tem;
  2624.  
  2625.   if (s->b == NULL)
  2626.     {
  2627.       if (n < 32)
  2628.     {
  2629.       n = 32;
  2630.     }
  2631.       s->p = s->b = xmalloc (n);
  2632.       s->e = s->b + n;
  2633.     }
  2634.   else if (s->e - s->p < n)
  2635.     {
  2636.       tem = s->p - s->b;
  2637.       n += tem;
  2638.       n *= 2;
  2639.       s->b = xrealloc (s->b, n);
  2640.       s->p = s->b + tem;
  2641.       s->e = s->b + n;
  2642.     }
  2643. }
  2644.  
  2645. static void
  2646. string_delete (s)
  2647.      string *s;
  2648. {
  2649.   if (s->b != NULL)
  2650.     {
  2651.       free (s->b);
  2652.       s->b = s->e = s->p = NULL;
  2653.     }
  2654. }
  2655.  
  2656. static void
  2657. string_init (s)
  2658.      string *s;
  2659. {
  2660.   s->b = s->p = s->e = NULL;
  2661. }
  2662.  
  2663. static void 
  2664. string_clear (s)
  2665.      string *s;
  2666. {
  2667.   s->p = s->b;
  2668. }
  2669.  
  2670. #if 0
  2671.  
  2672. static int
  2673. string_empty (s)
  2674.      string *s;
  2675. {
  2676.   return (s->b == s->p);
  2677. }
  2678.  
  2679. #endif
  2680.  
  2681. static void
  2682. string_append (p, s)
  2683.      string *p;
  2684.      CONST char *s;
  2685. {
  2686.   int n;
  2687.   if (s == NULL || *s == '\0')
  2688.     return;
  2689.   n = strlen (s);
  2690.   string_need (p, n);
  2691.   memcpy (p->p, s, n);
  2692.   p->p += n;
  2693. }
  2694.  
  2695. static void
  2696. string_appends (p, s)
  2697.      string *p, *s;
  2698. {
  2699.   int n;
  2700.  
  2701.   if (s->b != s->p)
  2702.     {
  2703.       n = s->p - s->b;
  2704.       string_need (p, n);
  2705.       memcpy (p->p, s->b, n);
  2706.       p->p += n;
  2707.     }
  2708. }
  2709.  
  2710. static void
  2711. string_appendn (p, s, n)
  2712.      string *p;
  2713.      CONST char *s;
  2714.      int n;
  2715. {
  2716.   if (n != 0)
  2717.     {
  2718.       string_need (p, n);
  2719.       memcpy (p->p, s, n);
  2720.       p->p += n;
  2721.     }
  2722. }
  2723.  
  2724. static void
  2725. string_prepend (p, s)
  2726.      string *p;
  2727.      CONST char *s;
  2728. {
  2729.   if (s != NULL && *s != '\0')
  2730.     {
  2731.       string_prependn (p, s, strlen (s));
  2732.     }
  2733. }
  2734.  
  2735. static void
  2736. string_prepends (p, s)
  2737.      string *p, *s;
  2738. {
  2739.   if (s->b != s->p)
  2740.     {
  2741.       string_prependn (p, s->b, s->p - s->b);
  2742.     }
  2743. }
  2744.  
  2745. static void
  2746. string_prependn (p, s, n)
  2747.      string *p;
  2748.      CONST char *s;
  2749.      int n;
  2750. {
  2751.   char *q;
  2752.  
  2753.   if (n != 0)
  2754.     {
  2755.       string_need (p, n);
  2756.       for (q = p->p - 1; q >= p->b; q--)
  2757.     {
  2758.       q[n] = q[0];
  2759.     }
  2760.       memcpy (p->b, s, n);
  2761.       p->p += n;
  2762.     }
  2763. }
  2764.  
  2765. /* To generate a standalone demangler program for testing purposes,
  2766.    just compile and link this file with -DMAIN and libiberty.a.  When
  2767.    run, it demangles each command line arg, or each stdin string, and
  2768.    prints the result on stdout. */
  2769.  
  2770. #ifdef MAIN
  2771.  
  2772. static void
  2773. demangle_it (mangled_name)
  2774.   char *mangled_name;
  2775. {
  2776.   char *result;
  2777.  
  2778.   result = cplus_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
  2779.   if (result == NULL)
  2780.     {
  2781.       printf ("%s\n", mangled_name);
  2782.     }
  2783.   else
  2784.     {
  2785.       printf ("%s\n", result);
  2786.       free (result);
  2787.     }
  2788. }
  2789.  
  2790. #include "getopt.h"
  2791.  
  2792. static char *program_name;
  2793. extern char *program_version;
  2794.  
  2795. static void
  2796. usage (stream, status)
  2797.      FILE *stream;
  2798.      int status;
  2799. {    
  2800.   fprintf (stream, "\
  2801. Usage: %s [-_] [-n] [-s {gnu,lucid,arm}] [--strip-underscores]\n\
  2802.        [--no-strip-underscores] [--format={gnu,lucid,arm}]\n\
  2803.        [--help] [--version] [arg...]\n",
  2804.        program_name);
  2805.   exit (status);
  2806. }
  2807.  
  2808. #define MBUF_SIZE 512
  2809. char mbuffer[MBUF_SIZE];
  2810.  
  2811. /* Defined in the automatically-generated ../binutils/underscore.c. */
  2812. extern int prepends_underscore;
  2813.  
  2814. int strip_underscore = 0;
  2815.  
  2816. static struct option long_options[] = {
  2817.   {"strip-underscores", no_argument, 0, '_'},
  2818.   {"format", required_argument, 0, 's'},
  2819.   {"help", no_argument, 0, 'h'},
  2820.   {"no-strip-underscores", no_argument, 0, 'n'},
  2821.   {"version", no_argument, 0, 'v'},
  2822.   {0, no_argument, 0, 0}
  2823. };
  2824.  
  2825. int
  2826. main (argc, argv)
  2827.      int argc;
  2828.      char **argv;
  2829. {
  2830.   char *result;
  2831.   int c;
  2832.  
  2833.   program_name = argv[0];
  2834.   xmalloc_set_program_name (program_name);
  2835.  
  2836.   strip_underscore = prepends_underscore;
  2837.  
  2838.   while ((c = getopt_long (argc, argv, "_ns:", long_options, (int *) 0)) != EOF)
  2839.     {
  2840.       switch (c)
  2841.     {
  2842.       case '?':
  2843.         usage (stderr, 1);
  2844.         break;
  2845.       case 'h':
  2846.         usage (stdout, 0);
  2847.       case 'n':
  2848.         strip_underscore = 0;
  2849.         break;
  2850.       case 'v':
  2851.         printf ("GNU %s version %s\n", program_name, program_version);
  2852.         exit (0);
  2853.       case '_':
  2854.         strip_underscore = 1;
  2855.         break;
  2856.       case 's':
  2857.         if (strcmp (optarg, "gnu") == 0)
  2858.           {
  2859.         current_demangling_style = gnu_demangling;
  2860.           }
  2861.         else if (strcmp (optarg, "lucid") == 0)
  2862.           {
  2863.         current_demangling_style = lucid_demangling;
  2864.           }
  2865.         else if (strcmp (optarg, "arm") == 0)
  2866.           {
  2867.         current_demangling_style = arm_demangling;
  2868.           }
  2869.         else
  2870.           {
  2871.         fprintf (stderr, "%s: unknown demangling style `%s'\n",
  2872.              program_name, optarg);
  2873.         exit (1);
  2874.           }
  2875.         break;
  2876.     }
  2877.     }
  2878.  
  2879.   if (optind < argc)
  2880.     {
  2881.       for ( ; optind < argc; optind++)
  2882.     {
  2883.       demangle_it (argv[optind]);
  2884.     }
  2885.     }
  2886.   else
  2887.     {
  2888.       for (;;)
  2889.     {
  2890.       int i = 0;
  2891.       c = getchar ();
  2892.       /* Try to read a label. */
  2893.       while (c != EOF && (isalnum(c) || c == '_' || c == '$' || c == '.'))
  2894.         {
  2895.           if (i >= MBUF_SIZE-1)
  2896.         break;
  2897.           mbuffer[i++] = c;
  2898.           c = getchar ();
  2899.         }
  2900.       if (i > 0)
  2901.         {
  2902.           int skip_first = strip_underscore && i > 1 && mbuffer[0] == '_';
  2903.           mbuffer[i] = 0;
  2904.           
  2905.           result = cplus_demangle (mbuffer+skip_first,
  2906.                        DMGL_PARAMS | DMGL_ANSI);
  2907.           if (result)
  2908.         {
  2909.           fputs (result, stdout);
  2910.           free (result);
  2911.         }
  2912.           else
  2913.         fputs (mbuffer + skip_first, stdout);
  2914.  
  2915.           fflush (stdout);
  2916.         }
  2917.       if (c == EOF)
  2918.         break;
  2919.       putchar (c);
  2920.     }
  2921.     }
  2922.  
  2923.   exit (0);
  2924. }
  2925.  
  2926. #endif    /* main */
  2927.